package com.eagledata.project.business.weeklyInfo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eagledata.common.constant.DictTypeConstans;
import com.eagledata.common.utils.StringUtil;
import com.eagledata.common.utils.security.ShiroUtils;
import com.eagledata.framework.config.EagleDataConfig;
import com.eagledata.project.business.weeklyInfo.domain.WeeklyInfo;
import com.eagledata.project.business.weeklyInfo.mapper.WeeklyInfoMapper;
import com.eagledata.project.system.dict.cache.DictCacheManager;

/**
 * M5周刊 服务层实现
 * 
 * @author eagledata
 * @date 2018-06-25
 */
@Service
public class WeeklyInfoServiceImpl implements IWeeklyInfoService 
{
	@Autowired
	private WeeklyInfoMapper weeklyInfoMapper;
	
	/**
     * 查询M5周刊信息
     * 
     * @param id M5周刊ID
     * @return M5周刊信息
     */
	 @Override
	public WeeklyInfo selectWeeklyInfoById(Integer id) {
	    return weeklyInfoMapper.selectWeeklyInfoById(id);
	}
	
	/**
     * 查询M5周刊列表
     * 
     * @param weeklyInfo M5周刊信息
     * @return M5周刊集合
     */
	 @Override
	public List<WeeklyInfo> selectWeeklyInfoList(WeeklyInfo weeklyInfo) {
		 List<WeeklyInfo> datas =  weeklyInfoMapper.selectWeeklyInfoList(weeklyInfo);
		 List<WeeklyInfo> resultData = new ArrayList<>();
		 Map<String, String> dictData = new HashMap<String, String>();
		 if(null!=weeklyInfo&&StringUtils.isNotEmpty(weeklyInfo.getWeeklyType())) {
			 String dictKey = "";
			 if("article".equals(weeklyInfo.getWeeklyType())){
				 dictKey = DictTypeConstans.M_INFORMATION_TYPE;
			 }else if("legend".equals(weeklyInfo.getWeeklyType())) {
				 dictKey = DictTypeConstans.M_LEGEND_TYPE;
			 }
			 dictData = DictCacheManager.getInstance().getDictData(dictKey);
		 }
		 for (WeeklyInfo data : datas) {
			 String imageUrl = StringUtils.join(EagleDataConfig.getBaseImgUrl(), data.getImageUrl());
			 data.setImageUrl(imageUrl);
			 data.setArticleType(dictData.get(data.getArticleType()));
			 resultData.add(data);
		}
	    return resultData;
	}
	
    /**
     * 新增M5周刊
     * 
     * @param weeklyInfo M5周刊信息
     * @return 结果
     */
	 @Override
	public int insertWeeklyInfo(WeeklyInfo weeklyInfo) {
	    return weeklyInfoMapper.insertWeeklyInfo(weeklyInfo);
	}
	
	/**
     * 修改M5周刊
     * 
     * @param weeklyInfo M5周刊信息
     * @return 结果
     */
	 @Override
	public int updateWeeklyInfo(WeeklyInfo weeklyInfo) {
	    return weeklyInfoMapper.updateWeeklyInfo(weeklyInfo);
	}
	
	/**
     * 保存M5周刊
     * 
     * @param weeklyInfo M5周刊信息
     * @return 结果
     */
	 @Override
	public int saveWeeklyInfo(WeeklyInfo weeklyInfo) {
	    Integer id = weeklyInfo.getId();
	    weeklyInfo.setCreateBy(ShiroUtils.getLoginName());
		int rows = 0;
		if (StringUtil.isNotNull(id)) {
		    rows = weeklyInfoMapper.updateWeeklyInfo(weeklyInfo);
		} else {
		    rows = weeklyInfoMapper.insertWeeklyInfo(weeklyInfo);
		}
		return rows;
	}
	
	/**
     * 删除M5周刊信息
     * 
     * @param id M5周刊ID
     * @return 结果
     */
	 @Override
	public int deleteWeeklyInfoById(Integer id) {
	    return weeklyInfoMapper.deleteWeeklyInfoById(id);
	}
	
	/**
     * 批量删除M5周刊对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	 @Override
	public int batchDeleteWeeklyInfo(Integer[] ids) {
		return weeklyInfoMapper.batchDeleteWeeklyInfo(ids);
	}
	
}
